﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using GurLoader;
using Microsoft.Win32;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;

namespace GurLoaderHelper
{
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.AllFiles)]
    public class SendToImgurExtensionOld : SharpContextMenu
    {
        public const string MailslotName = "GurLoader";
        
        private readonly IMessageSender messageSender = new MailslotMessageSender(MailslotName);

        private readonly object lockObj = new object();

        private List<string> ValidExtensions = new List<string>
        {
            ".gif",
            ".png",
            ".jpg",
            ".jpeg",
            ".pdf",
            ".bmp",
            ".tif",
            ".tiff"
        };

        public SendToImgurExtensionOld() : base()
        {
            
        }
        
        [ComRegisterFunction]
        private static void Register(Type registerType)
        {
            if (registerType != null)
            {
                using (RegistryKey clsidKey = Registry.ClassesRoot.OpenSubKey("CLSID"))
                {
                    using (RegistryKey guidKey = clsidKey.OpenSubKey(registerType.GUID.ToString("B"), true))
                    {
                        using (RegistryKey inproc = guidKey.OpenSubKey("InprocServer32", true))
                        {
                            inproc.SetValue("ThreadingModel", "Apartment", RegistryValueKind.String);
                        }
                    }
                }
            }
        }
        

        private IMessageSender MessageSender
        {
            get { return messageSender; }
        }

        protected override bool CanShowMenu()
        {
            MessageBox.Show(" Checking CanShotMenu");
            foreach (var selectedItemPath in SelectedItemPaths)
            {
                string ext = Path.GetExtension(selectedItemPath);
                if (string.IsNullOrEmpty(ext))
                    return false;

                ext = ext.ToLower();
                if (! ValidExtensions.Contains(ext))
                    return false;
            }
            return true;
        }

        protected override ContextMenuStrip CreateMenu()
        {
            var menu = new ContextMenuStrip();

            var topImgurMenu = new ToolStripMenuItem
            {
                Text = "Send To Imgur"
            };

            var sendImageMenuItem = new ToolStripMenuItem
            {
                Text = "Send Images"
            };

            sendImageMenuItem.Click += (sender, args) =>
            {
                ExecuteCommand(Command.SendImages);
            };

            var sendAlbumMenuItem = new ToolStripMenuItem
            {
                Text = "Send Album"
            };

            sendAlbumMenuItem.Click += (sender, arge) =>
            {
                ExecuteCommand(Command.SendAlbum);
            };


            topImgurMenu.DropDownItems.Add(sendImageMenuItem);
            topImgurMenu.DropDownItems.Add(sendAlbumMenuItem);

            menu.Items.Add(topImgurMenu);
            return menu;
        }

        private void ExecuteCommand(Command command)
        {
            lock (lockObj)
            {
                StringBuilder sb = new StringBuilder();

                sb.AppendFormat("{0} ", command);
                foreach (var selectedItemPath in SelectedItemPaths)
                {
                    sb.AppendFormat("\"{0}\" ", selectedItemPath);
                }

                MessageSender.SendMessage(sb.ToString());
            }
        }
    }
}
